package com.mlethe.android.demo.gesturelock;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mlethe.android.demo.R;
import com.mlethe.library.widget.gesturelock.GestureLockViewGroup;
import com.mlethe.library.widget.gesturelock.listener.GesturePasswordSettingListener;

public class GestureActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String PWD_MODE = "PWD_MODE";
    public static final int PWD_MODE_SETTING = 0x0011;
    public static final int PWD_MODE_VERIFY = 0x0012;
    public static final int PWD_MODE_UPDATE = 0x0013;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private int pwdMode;

    TextView resetTv;

    TextView pwdSettingHintTv;

    TextView pwdErrorHintTv;

    GestureLockViewGroup gestureLockView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture);
        resetTv = findViewById(R.id.resetTv);
        pwdSettingHintTv = findViewById(R.id.pwdSettingHintTv);
        pwdErrorHintTv = findViewById(R.id.pwdErrorHintTv);
        gestureLockView = findViewById(R.id.gestureLockView);
        pwdMode = getIntent().getIntExtra(PWD_MODE, PWD_MODE_SETTING);
        sp = getSharedPreferences("GESTURE", Context.MODE_PRIVATE);
        editor = sp.edit();
        String pwd = sp.getString(PWD_MODE, "");
        if (TextUtils.isEmpty(pwd)) {
            pwdMode = PWD_MODE_SETTING;
        } else if (pwdMode == PWD_MODE_VERIFY) {
            pwdSettingHintTv.setText("请绘制手势");
            gestureLockView.setPassword(pwd);
        } else if (pwdMode == PWD_MODE_UPDATE) {
            pwdSettingHintTv.setText("请输入旧手势密码");
            gestureLockView.setPassword(pwd);
        } else {
            finish();
            return;
        }
        resetTv.setOnClickListener(this);
        gestureLockView.setGestureEventListener((status, tryTimes) -> {
            if (status) {
                gestureLockView.removePassword();
                gestureLockView.resetView();
                pwdErrorHintTv.setText("");
                if (pwdMode == PWD_MODE_VERIFY) { // 验证手势
                    setResult(RESULT_OK);
                    finish();
                } else if (pwdMode == PWD_MODE_UPDATE) { // 修改手势
                    pwdSettingHintTv.setText("请设置新手势密码");
                } else {
                    pwdSettingHintTv.setText("请设置您的手势密码");
                }
            } else {
                pwdErrorHintTv.setText("密码错误，还可以再输入" + tryTimes + "次");
            }
        });
        gestureLockView.setGesturePasswordSettingListener(new GesturePasswordSettingListener() {
            @Override
            public void onFirstInputComplete(String text) {
                if (pwdMode == PWD_MODE_UPDATE) {
                    pwdSettingHintTv.setText("请确认新手势密码");
                } else {
                    pwdSettingHintTv.setText("请确认手势密码");
                }
                pwdErrorHintTv.setText("");
                resetTv.setVisibility(View.VISIBLE);
            }

            @Override
            public void onSuccess(String text) {
                editor.putString(PWD_MODE, text);
                editor.commit();
                Toast.makeText(GestureActivity.this, "设置成功，密码为：" + text, Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFail(String text) {
                pwdErrorHintTv.setText(text);
            }
        });

        gestureLockView.setGestureUnmatchedExceedListener(4, () -> {
            if (pwdMode == PWD_MODE_VERIFY) { // 验证手势
                finish();
            } else if (pwdMode == PWD_MODE_UPDATE) { // 修改手势
                finish();
            } else {
                gestureLockView.removePassword();
                gestureLockView.resetView();
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.resetTv) {
            resetTv.setVisibility(View.INVISIBLE);
            gestureLockView.removePassword();
            gestureLockView.resetView();
            pwdErrorHintTv.setText("");
            pwdSettingHintTv.setText("请设置您的手势密码");
        }
    }
}
